package service

import (
	"AiBus/global"
	"AiBus/model"
	"AiBus/utils"
	"github.com/gin-gonic/gin"
	"time"
)

func GetUserInfos(c *gin.Context, OpenID string) {

	userInfo := model.User{}
	tx := global.App.DB.Preload("Grants").Where("open_id=?", OpenID).First(&userInfo)
	if tx.Error != nil {
		utils.Fail(c, 403, "用户信息查询失败！")
	} else {
		utils.Success(c, 0, map[string]interface{}{
			"nick_name":  userInfo.NickName,
			"avatarUrl":  userInfo.AvatarUrl,
			"gender":     userInfo.Gender,
			"is_vip":     userInfo.IsVIP,
			"open_id":    userInfo.OpenId,
			"created_at": userInfo.CreatedAt.Format(time.DateTime),
			"grants": map[string]interface{}{
				"created_at":      userInfo.Grants.CreatedAt.Format(time.DateTime),
				"total_available": userInfo.Grants.TotalAvailable,
				"total_granted":   userInfo.Grants.TotalGranted,
				"updated_at":      userInfo.Grants.UpdatedAt.Format(time.DateTime),
				"user_id":         userInfo.Grants.UserId,
			},
		})
	}
}
